/*
 * Spring 2012 TCSS360 Deliverable 3
 * Matthew Selfors
 */

package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import storage.ProgramStateHandler;

import model.Auction;
import model.Bidder;
import model.CalendarModel;
import model.UserAccess;

@SuppressWarnings("serial")
public class AuctionListGUI extends JPanel {

	private CalendarModel calendar_model;

	private JFrame frame;

	private JPanel title;

	private JTextArea welcome;

	private JPanel auction_list;

	private JPanel auction_flow;

	private JTextArea auction;

	private JButton select;

	private JScrollPane scroll_pane;

	private Bidder bidder;

	private ProgramStateHandler my_ps;

	private UserAccess my_ua;

	public AuctionListGUI(
			final CalendarModel the_calendar_model,
			final Bidder the_bidder,
			final ProgramStateHandler ps,
			final UserAccess ua) {
		super();
		calendar_model = the_calendar_model;
		bidder = the_bidder;
		frame = new JFrame();
		frame.setPreferredSize(new Dimension(500, 600));
		frame.setResizable(false);
		frame.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(
					java.awt.event.WindowEvent e) {
				ps.write(new File("file.xml"),
						the_calendar_model, ua);
				System.exit(0);
			}
		});
		frame.add(this);
		setLayout(new BorderLayout());

		auction_list = new JPanel();
		auction_list.setLayout(new BoxLayout(auction_list,
				BoxLayout.PAGE_AXIS));
		auction_list.setBackground(Color.WHITE);
		scroll_pane = new JScrollPane(auction_list);
		scroll_pane.setVisible(true);
		scroll_pane.getVerticalScrollBar()
				.setUnitIncrement(20);

		setUpItemList();
		add(scroll_pane, BorderLayout.CENTER);

		setUpTitle();

		frame.setVisible(true);
		frame.pack();

		my_ps = ps;
		my_ua = ua;
		frame.setLocationRelativeTo(null);
	}

	private void setUpItemList() {
		String auction_title;
		SimpleDateFormat format =
				new SimpleDateFormat("MM/dd/yyyy");
		Font font = new Font("auction", 0, 18);

		for (Entry<Date, List<Auction>> entry_list : calendar_model
				.getAllCurrentAuctions().entrySet()) {
			for (int i = 0; i < entry_list.getValue()
					.size(); i++) {
				auction_flow = new JPanel(new FlowLayout());
				auction = new JTextArea();
				auction_title =
						entry_list.getValue().get(i)
								.getNPO()
								+ "\t"
								+ (format.format(entry_list
										.getValue().get(i)
										.getDate()) + "\t");
				auction.setText(auction_title);
				select = new JButton("Select");
				select.addActionListener(new SelectListener(
						entry_list.getValue().get(i)
								.getName(), entry_list
								.getKey()));
				auction.setFont(font);
				auction.setEditable(false);

				auction_flow.setBackground(Color.WHITE);
				auction_flow.add(auction);
				auction_flow.add(select);
				auction_list.add(auction_flow);
			}
		}
	}

	private void setUpTitle() {
		Font font = new Font("auction", 0, 18);
		title = new JPanel(new BorderLayout());
		welcome =
				new JTextArea("Welcome "
						+ bidder.getFirstName() + " "
						+ bidder.getLastName() + "!");
		JButton view_bids = new JButton("View bids");
		view_bids.addActionListener(new BidsListener());

		title.setBackground(Color.WHITE);
		welcome.setFont(font);
		title.add(welcome, BorderLayout.WEST);
		title.add(view_bids, BorderLayout.EAST);
		add(title, BorderLayout.NORTH);
	}

	private class SelectListener implements ActionListener {

		private String auction_name;

		private Date auction_date;

		public SelectListener(
				final String the_auction_name,
				final Date the_auction_date) {
			super();
			auction_name = the_auction_name;
			auction_date = the_auction_date;
		}

		@Override
		public void actionPerformed(
				final ActionEvent the_event) {
			AuctionGUI ag =
					new AuctionGUI(
							AuctionListGUI.this.calendar_model
									.getAuction(
											auction_name,
											auction_date),
							AuctionListGUI.this.bidder);
			ag.start(my_ps, calendar_model, my_ua);
		}
	}

	private class BidsListener implements ActionListener {

		@Override
		public void actionPerformed(
				final ActionEvent the_event) {
			new BidListGUI(AuctionListGUI.this.bidder);
		}
	}
}
